package com.chuanyang.driverh.mainmy;


import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AbsListView;
import android.widget.ListView;

import com.chuanyang.driverh.BaseApplication;
import com.chuanyang.driverh.R;
import com.chuanyang.driverh.base.BaseTitleActivity;
import com.chuanyang.driverh.base.OnCustomListener;
import com.chuanyang.driverh.mainmy.adapter.MemberAdapter;
import com.chuanyang.driverh.responseresult.CarTeamMenmberData;
import com.chuanyang.driverh.responseresult.CarTeamMenmberResult;
import com.chuanyang.driverh.responseresult.DriverInfoData;
import com.chuanyang.driverh.utils.ActivityUtil;
import com.chuanyang.driverh.utils.ConstUtil;
import com.chuanyang.driverh.utils.DialogUtil;
import com.chuanyang.driverh.utils.PrintUtil;
import com.chuanyang.driverh.utils.net.JsonGenericsSerializator;
import com.chuanyang.driverh.utils.net.MyCallback;
import com.chuanyang.driverh.utils.net.MyHttpUtil;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import okhttp3.Call;

public class SettingMemberActivity extends BaseTitleActivity {
    private MemberAdapter adapter;
    private ArrayList<CarTeamMenmberData> menmberlist;
    private DriverInfoData driverinfo;
    private ListView lv;

    @Override
    protected int getContentResId() {
        return R.layout.my_act_settingember;
    }

    @Override
    protected void initView(Bundle savedInstanceState) {
        lv = (ListView) findViewById(R.id.ll_act_friend);
    }

    @Override
    protected void initData() {
        setBackImg();
        getTitleTextView().setText("设置成员");
        getRightTextView().setText("申请");
        driverinfo = BaseApplication.getInstance().getDriverInfo();

        menmberlist = new ArrayList();
        adapter = new MemberAdapter(this, menmberlist);
        lv.setAdapter(adapter);
        getMenmber();
    }

    @Override
    protected void initListener() {
        // 设置ListView的滚动监听, 当用户上下拉ListView的时候把已经打开的条目关闭掉
        lv.setOnScrollListener(new AbsListView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(AbsListView view, int scrollState) {
                if (scrollState == SCROLL_STATE_TOUCH_SCROLL) {
                    // 触摸滑动, 关闭所有已开的条目
                    adapter.closeAllItems();
                }
            }
            @Override
            public void onScroll(AbsListView view, int firstVisibleItem,
                                 int visibleItemCount, int totalItemCount) {
            }
        });
        adapter.setOnCustomListener(new OnCustomListener() {//设置删除事件
            @Override
            public void onCustomerListener(View v, int position) {
                switch (v.getId()) {
                    case R.id.tv_delete:
                        deleteMenmber(position);
                        break;
                    case R.id.ll_div:
                        try {
                            Intent intent=new Intent(activityContext,DriverInfoActivity.class);
                            if (BaseApplication.getInstance().getDriverInfo().getDriver().getDriverType()==2) {
                                intent.putExtra("isCarTeamMenmber", true);
                            }else {
                                intent.putExtra("isCarTeamMenmber", false);
                            }
                            intent.putExtra("driverId", menmberlist.get(position).getDriverId() + "");
                            startActivity(intent);
                        } catch (Exception e) {
                            e.printStackTrace();
                        }

                        break;

                }
            }
        });
    }

    private void getMenmber() {
        DialogUtil.showLoadingDialog(activityContext,"加载中...");
        Map<String,String> params=new HashMap<String, String>();
        params.put("fleetId", driverinfo.getFleet().getId() + "");
        params.put("fleetDriverState", "1");
        MyHttpUtil.selectFleetDriver(activityContext,params, new MyCallback<CarTeamMenmberResult>(activityContext,new JsonGenericsSerializator()) {
            @Override
            public void onFail(Call call, Exception e, int id) {
                e.printStackTrace();
                PrintUtil.toast(activityContext, "网络异常，请稍后再试");
            }
            @Override
            public void onSuccess(CarTeamMenmberResult result, int id) {

                if (result.getCode() == ConstUtil.OK ) {
                    if (result.getData().size() > 0) {
                        menmberlist = result.getData();
                        adapter.setMenmberlist(result.getData());
                    }
                }
            }
        });

    }
    private void deleteMenmber(final int position) {
        DialogUtil.showLoadingDialog(activityContext,"删除队员...");
        Map<String,String> params=new HashMap<String, String>();
        params.put("fleetId", driverinfo.getFleet().getId() + "");
        params.put("fleetDriverState", "3");
        params.put("driverId", driverinfo.getDriverId()+"");
        MyHttpUtil.selectFleetDriver(activityContext,params, new MyCallback<CarTeamMenmberResult>(activityContext,new JsonGenericsSerializator()) {
            @Override
            public void onFail(Call call, Exception e, int id) {
                e.printStackTrace();
                PrintUtil.toast(activityContext, "网络异常，请稍后再试");
            }
            @Override
            public void onSuccess(CarTeamMenmberResult result, int id) {

                if (result.getCode() == ConstUtil.OK ) {
                    PrintUtil.toast(activityContext,"删除成功");
                    menmberlist.remove(position);
                    adapter.setMenmberlist(menmberlist);
                }
            }
        });
    }
    @Override
    protected void baseGoEnsure()
    {
        ActivityUtil.startActivity(activityContext,TeamMenmberApplicationActivity.class);
    }


}
